# Copyright (c) 2025 SenseTime. All Rights Reserved.
# Author: LazyLLM Team,  https://github.com/LazyAGI/LazyLLM

from sqlalchemy.sql import func
from utils.util_database import db
from models.model_account import Account


class Notification(db.Model):
    """通知数据模型"""
    __tablename__ = "notifications"

    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.String(255), nullable=False)  # 接收通知的用户
    tenant_id = db.Column(db.String(36), nullable=False)  # 租户ID
    title = db.Column(db.String(255), nullable=False)  # 通知标题
    content = db.Column(db.Text, nullable=False)  # 通知内容
    type = db.Column(db.String(50), default='info')  # 通知类型: info, warning, error, success
    is_read = db.Column(db.Boolean, default=False)  # 是否已读
    link = db.Column(db.String(512))  # 关联链接
    created_at = db.Column(db.DateTime, default=func.now())
    updated_at = db.Column(db.DateTime, default=func.now(), onupdate=func.now())

    @property
    def creator(self):
        """获取创建者名称"""
        account = Account.query.filter_by(id=self.user_id).first()
        return account.name if account else "Unknown"

    def to_dict(self):
        """转换为字典"""
        return {
            'id': self.id,
            'user_id': self.user_id,
            'tenant_id': self.tenant_id,
            'title': self.title,
            'content': self.content,
            'type': self.type,
            'is_read': self.is_read,
            'link': self.link,
            'created_at': self.created_at.isoformat() if self.created_at else None,
            'updated_at': self.updated_at.isoformat() if self.updated_at else None,
        }
